using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using GetStartedApp.Models;
using GetStartedApp.Pages;
using GetStartedApp.Pages.Controls;
using GetStartedApp.Pages.Layouts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GetStartedApp.ViewModel;

/// <summary>
/// 主窗体
/// </summary>
public partial class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        CreateMenuBars();
        CurrentPage = new IndexPageView();
    }

    /// <summary>
    /// 菜单集合
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<MenuBar> _menuBars;

    /// <summary>
    /// 选择的页面
    /// </summary>
    [ObservableProperty]
    private MenuBar? _selectedListItem;

    /// <summary>
    /// 当前页面
    /// </summary>
    [ObservableProperty]
    private UserControl _currentPage;

    /// <summary>
    /// 创建导航列表
    /// </summary>
    private void CreateMenuBars()
    {
        MenuBars =
        [
            new MenuBar
            {
                Title = "控件",
                Child =
                [
                    new MenuBar("Border", typeof(BorderPageView)),
                    new MenuBar("Checkbox", typeof(CheckBoxPageView)),
                    new MenuBar("ComboBox", typeof(ComboBoxPageView)),
                    new MenuBar("Button", typeof(ButtonStylePageView)),
                    new MenuBar("ListBox", typeof(ListBoxPageView)),
                    new MenuBar("ItemsPanel", typeof(ItemsPanelPageView)),
                    new MenuBar("TabStrip", typeof(TabStripPageView)),
                    new MenuBar("DataTemplate", typeof(DataTemplatePageView)),
                    new MenuBar("TabControl", typeof(TabControlPageView)),
                    new MenuBar("Menu", typeof(MenuPageView)),
                    new MenuBar("TreeView", typeof(TreeViewPageView)),
                    new MenuBar("DataGrid", typeof(DataGridPageView)),
                ],
            },
            new MenuBar
            {
                Title = "布局",
                Child =
                [
                    new MenuBar("Grid", typeof(GridPageView)),
                    new MenuBar("StarkPanel", typeof(StarkPanelPageView)),
                    new MenuBar("Grid Form", typeof(GridFormPageView))
                ],
            },
            new MenuBar
            {
                Title = "操作",
                Child =
                [
                    new MenuBar("Bind", typeof(BindPageView)),
                    new MenuBar("Command", typeof(CommandPageView)),
                    new MenuBar("Trigger", typeof(TriggerPageView)),
                    new MenuBar("SelectFiles", typeof(SelectedFilesView))
                ],
            }
        ];
    }

    /// <summary>
    /// 页面切换
    /// </summary>
    /// <param name="value"></param>
    partial void OnSelectedListItemChanged(MenuBar? value)
    {
        if (value?.MenuType is null)
            return;

        var service = Activator.CreateInstance(value.MenuType);
        if (service is null)
            return;

        CurrentPage = (UserControl)service;
    }
}